<?php
namespace App\Http\Controllers\Order;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\RedPacket\RedPacket;
use App\Models\RedPacket\RedPacketRecord;


class RedPacketController extends Controller
{
    public function index(Request $request) 
    {
        if ($request->ajax()) {

            $rows = $request->get("rows");

            $Packets = new RedPacket();

            $Packets = $this->filters($request, $Packets);

            $paginator = $Packets->orderBy($request->get("sidx"), $request->get("sord"))->paginate($rows);

            $rows = [];
            foreach ($paginator as $page) {
                $rows[] = $page;
            }

            $data = [
                "rows" => $rows,
                "total"=> $paginator->lastPage(),
                "page" => $paginator->currentPage(),
                "records" => $paginator->count(),
            ];

            return response()->json($data);
            
        } else {

            $rs = "1:默认随机红包";
            $sum1 = RedPacket::select(DB::raw("SUM(total_amount) as sum1"))->first()->sum1;
            $sum2 = RedPacket::select(DB::raw("SUM(send_amount) as sum2"))->first()->sum2;

            return view("order/redpacket/index",[
                "rs"      => $rs,
                "sum1" =>$sum1,
                "sum2"=>$sum2
            ]);
        }
    }

    public function edit(Request $request)
    {
        $oper = $request->get('oper');

        if ($oper == "add"){
            $this->validate($request, [
                "count_down"  => "required|date|after_or_equal:today",
                "begin_time"  => "required|date|after:count_down",
                "min_money"   =>  "required|integer|min:1",
                "num"         =>  "required|integer|min:1",
                "total_amount"=>  "required|integer|min:1",
            ]);

            $is_run = RedPacket::where("is_run", 1)->first();
            if ($is_run){
                return response()->json(["success" => false , "message" => "请先结束上一场红包活动"]);
            }
            $count_down = $request->get("count_down");
            $begin_time = $request->get("begin_time");
            $num = $request->get("num");
            $min_money = $request->get("min_money"); //分
            $total = $request->get("total_amount") * 100; //分

            if(strtotime($begin_time) < time()){
                return response()->json(["success" => false , "message" => "开始时间不能小于当前时间"]);
            }
            if ($total < ($min_money * $num)) {
                return response()->json(["success" => false , "message" => "红包总金额不能小于最小金额*数量"]);
            }

            $packet = new RedPacket();

            $packet->num = $num;
            $packet->total_amount =$total;
            $packet->min_money =$min_money;
            $packet->count_down =$count_down;
            $packet->begin_time =$begin_time;
            $packet->is_run = 1;
            $packet->type = 1;
            $packet->save();
            
            return response()->json(["success" => true, "message" => "修改数据成功"]);
        }elseif ($oper == "edit"){
            $this->validate($request, [
                "id"          => "required|integer",
                "count_down"  => "required|date|after_or_equal:today",
                "begin_time"  => "required|date|after:count_down",
                "min_money"   => "required|integer|min:1",
                "num"         => "required|integer|min:1",
                "total_amount"=> "required|integer|min:1",
                
            ]);

            $packet = RedPacket::find($request->get("id"));

            if (!$packet || !$packet->is_run){
                return response()->json(["success" => false , "message" => "本场红包已结束,不能修改"]);
            }

            $count_down = $request->get("count_down");
            $begin_time = $request->get("begin_time");
            $num = $request->get("num");
            $min_money = $request->get("min_money");
            $total = $request->get("total_amount") * 100;

            if ($packet->is_run == 1 && time() > strtotime($packet->begin_time)){
                return response()->json(["success" => false , "message" => "红包正在发放,不能修改"]);
            }
            if(strtotime($begin_time) < time()){
                return response()->json(["success" => false , "message" => "开始时间不能小于当前时间"]);
            }
            if ($total < ($min_money * $num)) {
                return response()->json(["success" => false , "message" => "红包总金额不能小于最小金额*数量"]);
            }

            $packet->num = $num;
            $packet->total_amount =$total;
            $packet->min_money =$min_money;
            $packet->count_down =$count_down;
            $packet->begin_time =$begin_time;
            $packet->type = 1;
            $packet->save();
            
            return response()->json(["success" => true, "message" => "修改数据成功"]);
                $packet = RedPacket::find($request->get("id"));
                if ($packet->is_run){
                    return response()->json(["success" => false , "message" => "红包已打开,不能修改"]);
                }
        }elseif($oper == "del"){
            $this->validate($request, [
                "id" => "required|integer"
            ]);

            $packet = RedPacket::find($request->get("id"));
            if ($packet){
                $packet->delete();
                return response()->json(["success" => true]);
            }else{
                return response()->json(["success" => false , "message" => "删除失败"]);
            }
        }else {
            
        }
    }

    public function closePacket(Request $request)
    {
        $this->validate($request, [
            "id" => "required|integer"
        ]);
        $id = (int)$request->get("id");
        
        $packet = RedPacket::find($id);
 
        if ($packet && $packet->is_run == 1){
            $packet->is_run = 0;
            $packet->save();
            return response()->json(["success" => true]);
        }else{
            return redirect()->back()->withErrors("参数错误");
        }
    }
}
